#http://www.geocities.com/permute_it/
#Scalable Permutations with no recursion, and other stuff

def printarray(array):
    for i in array:
        print i, " ",
    print 

def main():
    cards = [1,2,3,4]
    n = len(cards)

    control = []
    for i in range(n+1):
        control.append(n)

    printarray(cards)

    i = 1
    j = 0

    while i < n:
        control[i] -= 1
        if i%2 == 1:
            j = control[i]
        else:
            j = 0

        cards[j], cards[i] = cards[i], cards[j]
        i = 1
        while control[i] == 0:
            control[i] = i
            i += 1
            printarray(cards)


    printarray(cards)

main()
